import { getClassifyList, addCollect } from '@/api'
import { getRange } from '@/utils/helpers'
import store from '@/store'
import { Message } from 'element-ui'

export default function (editor) {
  // 收藏
  editor.ui.registry.addToggleMenuItem('contextmenu_collect', {
    text: '收藏',
    async onAction() {
      const { docContent: _jsonContent } = getRange('collect')
      await getClassifyList()
      const items = store.state.xmleditor.classifyList.map(i => {
        return { value: `${i.id}`, text: i.name }
      })

      editor.windowManager.open({
        title: '收藏',
        body: {
          type: 'panel',
          items: [
            {
              type: 'input',
              name: 'name',
              label: '名称'
            },
            {

              type: 'selectbox',
              name: 'classifyId',
              label: '类型',
              items
            }
          ]
        },
        buttons: [
          {
            type: 'submit',
            name: 'submitButton',
            text: '确定',
            buttonType: 'primary'
          },
          {
            type: 'cancel',
            name: 'closeButton',
            text: '取消'
          }
        ],
        initialData: {
          name: '',
          classifyId: "-1"
        },
        async onSubmit(api) {
          const { name, classifyId } = api.getData();
          if(!name || !name.trim()) {
            Message.warning('请输入名称')
            return
          }
          const { docContent: jsonContent } = getRange('collect')
          const docNo = store.state.xmleditor.editorConfig.docNo
          await addCollect({ name, classifyId: +classifyId, docNo, jsonContent })
          tinymce.activeEditor.notificationManager.open({
            timeout: 1500,
            text: '保存成功!',
            type: 'success'
          })
          api.close();
        }
      })
    }
  })

  // console.debug('---右键菜单: 个人收藏---')
}
